# This is a sample Python script.
import json

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import requests
import copy


#1：管理员 2：项目经理 3：总施工 4：安全员 5：施工员 6:材料员 7：仓库管理员 8：工头 9:工人 10：保安
# roleId = [1,2,3,4,5,6,7,8,9,10]
# roles = [[19,25,31,37,43,49,55,61,67,73,79,85,91,97,103,115,121,127,133,139,145,151,157,163,169,175,181,187,193,199,205,211,217,223,229,235,241,247,253,259,265,271,277,283,289,295,307,313,319,325,331,337,343,349,355,361],
#          [19,25,31,37,43,49,55,61,67,73,79,85,91,97,103,115,121,127,133,139,145,151,157,163,169,175,181,187,193,199,205,211,217,223,229,235,241,247,253,259,265,271,277,289,295,307,313,319,325,331,337,343,349],
#          [19,25,31,37,43,49,55,61,67,73,79,85,91,97,103,115,121,127,133,139,145,151,157,163,169,175,181,187,193,199,205,211,217,223,229,235,241,247,253,259,265,271,277,289,295,307,313,319,325,331,337,343,349],
#          [19,25,31,37,43,73,85,97,103,121,127,133,139,145,151,157,163,175,187,193,199,217,223,229,235,241,247,253,259,265,271,277,295,319,325,331,337,343,349],
#          [19,25,31,37,43,73,85,97,121,133,139,145,151,163,181,205,211,235,241,247,253,265,271,277,295,319,325,331,337,343,349],
#          [43,55,97,133,139,229,241,253,259,271,277,283,289],
#          [43,79,97,133,139,151,163,169,187,229,235,241,247,253,259,277,283,289,319,325,331,337,343,349],
#          [25,37,43,61,91,97,139,163,277,283,325,343],
#          [31,43,115,193,277,283,331,349],
#          [115,277,307]]
# roleIds = []
# for item1,item2 in zip(roleId,roles):
#     item2.insert(0,item1)
#     print(item2)
#     roleIds.append(item2)
#     url = "https://192.168.3.82:8097/api/system/user"

class Test:
    role_dict = {}
    # 1：管理员 2：项目经理 3：总施工 4：安全员 5：施工员 6:材料员 7：仓库管理员 8：工头 9:工人 10：保安
    role_dict[1] = [19,25,31,37,43,49,55,61,67,73,79,85,91,97,103,115,121,127,133,139,145,151,157,163,169,175,181,187,193,199,205,211,217,223,229,235,241,247,253,259,265,271,277,283,289,295,307,313,319,325,331,337,343,349,355,361]
    role_dict[2] = [19,25,31,37,43,49,55,61,67,73,79,85,91,97,103,115,121,127,133,139,145,151,157,163,169,175,181,187,193,199,205,211,217,223,229,235,241,247,253,259,265,271,277,289,295,307,313,319,325,331,337,343,349]
    role_dict[3] = [19,25,31,37,43,49,55,61,67,73,79,85,91,97,103,115,121,127,133,139,145,151,157,163,169,175,181,187,193,199,205,211,217,223,229,235,241,247,253,259,265,271,277,289,295,307,313,319,325,331,337,343,349]
    role_dict[4] = [19,25,31,37,43,73,85,97,103,121,127,133,139,145,151,157,163,175,187,193,199,217,223,229,235,241,247,253,259,265,271,277,295,319,325,331,337,343,349]
    role_dict[5] = [19,25,31,37,43,73,85,97,121,133,139,145,151,163,181,205,211,235,241,247,253,265,271,277,295,319,325,331,337,343,349]
    role_dict[6] = [43,55,97,133,139,229,241,253,259,271,277,283,289]
    role_dict[7] = [43,79,97,133,139,151,163,169,187,229,235,241,247,253,259,277,283,289,319,325,331,337,343,349]
    role_dict[8] = [25,37,43,61,91,97,139,163,277,283,325,343]
    role_dict[9] = [31,43,115,193,277,283,331,349]
    role_dict[10] = [115,277,307]

    role_dictT = copy.deepcopy(role_dict)


    # "userId":853  用户编号
    # "memberId": "900000202312181055" 用户身份证
    # "userName":"13735438002_U" 用户名
    # "nickName":"夏测试2" 用户昵称
    # "phonenumber":"13735438002" 手机号
    # "roles":[43,55,97,133,139,229,241,253,259,271,277,283,289] 权限
    # "roleIds":[6,43,55,97,133,139,229,241,253,259,271,277,283,289] 角色+权限
    # id 表示角色id
    def __init__(self,phoneNumber,id,url,adminToken,adminPass):
        self.phoneNumber = phoneNumber
        self.roles = self.role_dict[id]
        self.role_dictT[id].insert(0,id)   #将角色id插入到权限列表组成roleIDs
        self.roleIds = self.role_dictT[id]
        self.url = url
        self.adminToken = adminToken
        self.adminPass = adminPass

    def reqUserMessage(self):
        #用户管理-搜索手机号码接口
        path = "/list?pageNum=1&pageSize=10&nickName=&phonenumber={}&beginTime=&endTime=".format(self.phoneNumber)
        url = self.url + path
        print(url)
        payload={}
        headers = {
            'Accept': 'application/json',
            'Authorization': 'Bearer ' + self.adminToken,
            'Content-Type': 'application/json;charset=UTF-8',
            'Cookie': 'username=admin; rememberMe=true; password=' + self.adminPass + '; Admin-Token=' + self.adminToken + '; sidebarStatus=0',
            'Referer': 'https://192.168.3.82:8097/system/user',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        }
        response = requests.request("GET", url, headers=headers, data=payload,verify=False)
        memberId = response.json()["rows"][0]["memberId"]
        userName = response.json()["rows"][0]["userName"]
        nickName = response.json()["rows"][0]["nickName"]
        userId = response.json()["rows"][0]["userId"]
        return userId, memberId, userName, nickName



    #更改用户权限
    def changePermi(self, userId, memberId, userName, nickName):
        payload = (
            '{{"searchValue":null,"createBy":"admin","createTime":"2023-12-15 16:43:25","updateBy":null,"updateTime":"2023-12-15 17:30:07","remark":null,"beginTime":null,"endTime":null,"params":{{}},'
            '"actionType":null,"rowNum":null,"pushFlag":0,"userId":{useId},"pushId":null,"roleId":null,"roleKey":null,"loginName":null,"memberId":{memberId},"deptId":null,"userName":"{userName}",'
            '"ifUpdateRoles":null,"nickName":{nickName},"email":"","phonenumber":"{phonenumber}","companyName":" ","sex":"1","avatar":"","salt":null,"status":"0","delFlag":"0","loginIp":"","loginDate":null,'
            '"roles":{roles},"roleIds":{roleIds},"admin":false,"password":"","largeRoles":[10]}}')
        #self.nickName = json.dumps(self.nickName,ensure_ascii=False)  #昵称存在中文需要确保ensure_ascii=False
        payload_str = payload.format(useId=userId, memberId=memberId, roles=self.roles, roleIds=self.roleIds, userName=userName, phonenumber=self.phoneNumber,nickName=json.dumps(nickName,ensure_ascii=False))

        #payload_json = json.loads(payload_str)
        payload_str = payload_str.encode('utf-8')
        print(payload_str)

        headers = {
            'Accept': 'application/json',
            'Authorization': 'Bearer '+self.adminToken,
            'Content-Type': 'application/json;charset=UTF-8',
            'Cookie': 'username=admin; rememberMe=true; password='+self.adminPass+'; Admin-Token='+self.adminToken+'; sidebarStatus=0',
            'Referer': 'https://192.168.3.82:8097/system/user',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        }
        print(headers)

        response = requests.request("PUT", self.url, headers=headers, data=payload_str, verify=False)

        print(response.text)

if __name__ == "__main__":
    # useId = 850
    # memberId = '900000202312181052'
    # userName = '13735438001_U'
    # nickName = '夏测试1-工头'
    phoneNumber = '13735438001'
    id = 10
    url = "https://192.168.3.82:8097/api/system/user"   #修改用户权限接口
    adminToken = "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjBiODU1ZTUyLTQyOTQtNGMyOC1hNWJlLWRhMTUzZTExN2M5NCJ9.HRSLl-XmFZQpZ4hLPaKV6_NTynryFWFGC4vDL2kQ8kiqZQMwLvE-XMgVz5Kd4IsjC_FRgwYHo1JuU9Td4KleSw"
    adminPassword = "BWTpJHhFcXR7pkBr2P6bnnquMRvF+tg7BBuePl5c0r4bb/NJLcD1w4xftft+nyh3RX3Er9m9Nrn0+v4KAbze+g=="

    test = Test(phoneNumber, id, url, adminToken, adminPassword)
    userId, memberId, userName, nickName = test.reqUserMessage()
    print(userId, memberId, userName, nickName)
    test.changePermi(userId, memberId, userName, nickName)








    